﻿using MediatR;
using Order.Api.Read.Application.Command;
using Order.Api.Read.Dto.Order;
using Order.Api.Read.Service.RefundService;
using Order.ErrorCode;
using Order.Domain.OrderDomain;
using AutoMapper;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Order.Api.Read.Application.Handler
{
    /// <summary>
    /// 退款分页查询处理器
    /// 处理退款分页查询请求
    /// </summary>
    public class RefundPageHandler : IRequestHandler<RefundPageCommand, ApiPageIng<RefundPageDto>>
    {
        private readonly IRefundService _refundService;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="refundService">退款服务</param>
        /// <param name="mapper">对象映射器</param>
        public RefundPageHandler(IRefundService refundService, IMapper mapper)
        {
            _refundService = refundService ?? throw new ArgumentNullException(nameof(refundService), "退款服务不能为空");
            _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper), "对象映射器不能为空");
        }

        /// <summary>
        /// 处理退款分页查询请求
        /// </summary>
        /// <param name="request">退款分页查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>分页结果</returns>
        /// <exception cref="ArgumentNullException">当request为null时抛出</exception>
        public async Task<ApiPageIng<RefundPageDto>> Handle(RefundPageCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 参数验证
                if (request == null)
                {
                    throw new ArgumentNullException(nameof(request), "查询请求不能为空");
                }

                // 调用服务层方法获取分页数据
                var result = await _refundService.GetRefundPageAsync(request);

                // 映射到DTO
                var refundDtos = _mapper.Map<List<RefundPageDto>>(result.Data);

                return new ApiPageIng<RefundPageDto>
                {
                    Data = refundDtos,
                    TotalCount = result.TotalCount,
                    PageIndex = result.PageIndex,
                    PageSize = result.PageSize
                };
            }
            catch (Exception ex)
            {
                // 记录异常日志
                // TODO: 添加日志记录
                throw new Exception($"处理退款分页查询请求时发生错误: {ex.Message}", ex);
            }
        }
    }

    /// <summary>
    /// 退款详情查询处理器
    /// 处理退款详情查询请求
    /// </summary>
    public class RefundDetailHandler : IRequestHandler<RefundDetailCommand, RefundDetailDto>
    {
        private readonly IRefundService _refundService;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="refundService">退款服务</param>
        /// <param name="mapper">对象映射器</param>
        public RefundDetailHandler(IRefundService refundService, IMapper mapper)
        {
            _refundService = refundService ?? throw new ArgumentNullException(nameof(refundService), "退款服务不能为空");
            _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper), "对象映射器不能为空");
        }

        /// <summary>
        /// 处理退款详情查询请求
        /// </summary>
        /// <param name="request">退款详情查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>退款详情DTO</returns>
        /// <exception cref="ArgumentNullException">当request为null时抛出</exception>
        public async Task<RefundDetailDto> Handle(RefundDetailCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 参数验证
                if (request == null)
                {
                    throw new ArgumentNullException(nameof(request), "查询请求不能为空");
                }

                // 验证退款ID
                if (request.Id <= 0)
                {
                    throw new ArgumentException("退款ID必须大于0", nameof(request.Id));
                }

                // 调用服务层方法获取退款详情
                var refundDetail = await _refundService.GetRefundDetailAsync(request.Id);
                return refundDetail;
            }
            catch (Exception ex)
            {
                // 记录异常日志
                // TODO: 添加日志记录
                throw new Exception($"处理退款详情查询请求时发生错误: {ex.Message}", ex);
            }
        }
    }
}
